package com.road.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.road.model.Comment;
import com.road.model.Msg;
import com.road.model.See;
import com.road.service.ICommentService;
import com.road.service.INewsService;
import com.road.service.imp.CommentServiceImp;
import com.road.service.imp.NewsServiceImp;
import com.road.utils.Consists;
import com.road.utils.ResponseUtil;

/**
 * Servlet implementation class SeeServlet
 */
@WebServlet(name = "comment", description = "评论", urlPatterns = { "/comment" })
public class CommentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ICommentService iService=new CommentServiceImp();
    List<Object> list2=null;
    Msg msg=null;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		list2=new ArrayList<>();
		msg=new Msg();
		List<See> list=null;
		Map<String, Object> map=null;
		try {
	        //更新获取评论列表	1，新增评论2	删除评论3
			if(request.getParameter("action").equals("1")) {
				getCommentList(request);
			}else if (request.getParameter("action").equals("2")) {
				saveComment(request, list);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			msg.setCode(Consists.Error_Code_ServerError);
			msg.setMsg(Consists.Error_Msg_ServerError);
			list2.add(msg);
		}
		
		ResponseUtil.sendScript(response, JSON.toJSONString(list2));
	}

	private void saveComment(HttpServletRequest request, List<See> list) throws SQLException {
		int i = iService.saveComment(request.getParameter("phone"), request.getParameter("news_id"), request.getParameter("content"));
		if(i>0) {
			msg.setCode(Consists.Error_Code_OK);
			msg.setMsg(Consists.Error_Msg_HANDLER_OK);
		}else {
			msg.setCode(Consists.Error_Code_FAIL);
			msg.setMsg(Consists.Error_Msg_HANDLER_FAIL);
		}
		list2.add(msg);
		list2.add(list);
	}

	private void getCommentList(HttpServletRequest request) throws SQLException {
		List<Comment> times = iService.getCommentListByNewsId(request.getParameter("news_id"));
		 if(times!=null&&times.size()>0) {
				msg.setCode(Consists.Error_Code_OK);
				msg.setMsg(Consists.Error_Msg_HANDLER_OK);
			}else {
				msg.setCode(Consists.Error_Code_FAIL);
				msg.setMsg(Consists.Error_Msg_HANDLER_FAIL);
			}
		 list2.add(msg);
		 list2.add(times);
	}

	

}
